﻿using System.Globalization;
using Yz.Base;
namespace Yz.Core.Extensions
{
    public static class DateTimeExtension
    {
        /// <summary>
        ///     根据传入时间获取周一时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetStartWeek(this DateTime dt, DayOfWeek lastDayofWeek = DayOfWeek.Saturday)
        {
            var addDay = Convert.ToInt32(dt.DayOfWeek.ToString("d"));
            if (lastDayofWeek == DayOfWeek.Sunday) //结束时间是周日
                addDay -= 1;
            var startWeek = dt.Date.AddDays(-addDay);
            return startWeek;
        }

        /// <summary>
        ///     根据传入时间获取月初时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetStartMonth(this DateTime dt)
        {
            var startMonth = dt.Date.AddDays(1 - dt.Day); //本月月初
            return startMonth;
        }

        /// <summary>
        ///     根据传入时间获取月末时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetLastDayOfMonth(this DateTime dt)
        {
            var month = dt.Date.AddDays(1 - dt.Day).AddMonths(1).AddDays(-1); //本月月末
            return month;
        }

        /// <summary>
        ///     根据传入时间获取本月的第几周
        /// </summary>
        /// <param name="daytime"></param>
        /// <returns></returns>
        public static int GetWeekNumInMonth(this DateTime daytime)
        {
            var dayInMonth = daytime.Day;
            //本月第一天
            var firstDay = daytime.AddDays(1 - daytime.Day);
            //本月第一天是周几
            var weekday = firstDay.DayOfWeek == 0 ? 7 : (int)firstDay.DayOfWeek;
            //本月第一周有几天
            var firstWeekEndDay = 7 - (weekday - 1);
            //当前日期和第一周之差
            var diffday = dayInMonth - firstWeekEndDay;
            diffday = diffday > 0 ? diffday : 1;
            //当前是第几周,如果整除7就减一天
            var WeekNumInMonth = (diffday % 7 == 0
                                     ? diffday / 7 - 1
                                     : diffday / 7) + 1 + (dayInMonth > firstWeekEndDay ? 1 : 0);
            return WeekNumInMonth;
        }

        /// <summary>
        ///     根据传入时间获取本年的第几周
        /// </summary>
        /// <param name="daytime"></param>
        /// <returns></returns>
        public static int GetWeekNumInYear(this DateTime daytime)
        {
            var gc = new GregorianCalendar();
            var weekOfYear = gc.GetWeekOfYear(daytime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
            return weekOfYear;
        }

        /// <summary>
        ///     获取日期当天的最终时刻
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime EndOfDay(this DateTime dt)
        {
            var endTime = dt.Date.AddDays(1).AddSeconds(-1);
            return endTime;
        }

        /// <summary>
        /// 获取格式化字符串 
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToDateTimeString(this DateTime dateTime, string dateFormat = DateFormat.yMd)
        {
            return dateTime.ToString(dateFormat);
        }
    }
}
